package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.WwOperateAppInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.WwOperateSelectAppDTO;
import com.itlong.cloud.POJO.DTO.operate.WwOperateStatusDTO;
import com.itlong.cloud.POJO.PO.WwAppPO;
import com.itlong.cloud.POJO.VO.operate.WwOperateAppInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IOperateAppDao;
import com.itlong.cloud.operate.service.IOperateAppService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 * :
 * <desc>
 *
 * @author LiChangXu
 * @create Date 2021-08-18
 **/
@Service
public class OperateAppServiceImpl implements IOperateAppService {

    @Autowired
    private IOperateAppDao iOperateAppDao;

    @Override
    public List<WwAppPO> getList() {
        return iOperateAppDao.getList();
    }

    @Override
    public Integer saveApp(WwOperateAppInfoDTO wwOperateAppInfoDTO) throws Exception{
        WwAppPO wwAppPO = new WwAppPO();
        BeanUtils.copyProperties(wwOperateAppInfoDTO,wwAppPO);
        wwAppPO.setAppId(LogicIdUtil.bussinessId());
        wwAppPO.setCreateTime(new Date());
        Map<String,Object> param = SqlUtil.durableData(wwAppPO,PlatformConstants.TABLE_SAVE);
        param.put("appId",wwAppPO.getAppId());
        return iOperateAppDao.saveApp(param);
    }

    @Override
    public Integer updateApp(WwOperateAppInfoDTO wwOperateAppInfoDTO) throws Exception{
        WwAppPO wwAppPO = new WwAppPO();
        BeanUtils.copyProperties(wwOperateAppInfoDTO,wwAppPO);
        wwAppPO.setUpdateTime(new Date());
        Map<String,Object> param = SqlUtil.durableData(wwAppPO,PlatformConstants.TABLE_UPDATE);
        return iOperateAppDao.updateApp(param);
    }

    @Override
    public Page<WwOperateAppInfoVO> getAppInfoPageList(WwOperateSelectAppDTO wwOperateSelectAppDTO) throws Exception{
        Page<WwOperateAppInfoVO> voPage = new Page<>();
        voPage.getPage().setPageSize(wwOperateSelectAppDTO.getPageSize());
        voPage.getPage().setCurrentPage(wwOperateSelectAppDTO.getCurrentPage());
        Map<String,Object> mapDto = SqlUtil.convertBeanToMapParams(wwOperateSelectAppDTO);
        mapDto.put("startSlip",voPage.getPage().getStartSlip());
        mapDto.put("pageSize",voPage.getPage().getPageSize());
        voPage.setRows(iOperateAppDao.getAppInfoPageList(mapDto),iOperateAppDao.getPageCount(mapDto));
        return voPage;
    }

    @Override
    public Integer updateStatus(WwOperateStatusDTO wwOperateStatusDTO) throws Exception{
        WwAppPO wwAppPO = new WwAppPO();
        BeanUtils.copyProperties(wwOperateStatusDTO,wwAppPO);
        wwAppPO.setUpdateTime(new Date());
        Map<String,Object> param = SqlUtil.durableData(wwAppPO,PlatformConstants.TABLE_UPDATE);
        return iOperateAppDao.updateStatus(param);
    }
}
